package com.zhoug.widget.round;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;

import com.zhoug.widget.R;

/**
 * 圆形按钮
 * 使用widgets_roundButtonStyle在theme中定义全局属性
 * @Author 35574
 * @Date 2020/7/2
 * @Description
 */
public class RoundButton extends androidx.appcompat.widget.AppCompatButton {
    private static final String TAG = ">>>RoundButton";
    private RoundHelper roundHelper;
    /**
     * 是否启用圆角
     */
    private boolean roundEnable = true;
    private boolean pressed = false;
    private int bgColor;
    private int bgColorPress;
    private int borderWidth;
    private int borderColor;
    private int borderColorPress;

    public RoundButton(Context context) {
//        super(context);
        this(context, null,0);
    }

    public RoundButton(Context context, AttributeSet attrs) {
//        super(context, attrs);
        this(context, attrs,0);
    }

    public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs,defStyleAttr);
        super(context, attrs, R.attr.widgets_roundButtonStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundButton,R.attr.widgets_roundButtonStyle,R.style.widgets_roundButtonStyle);

        roundEnable = typedArray.getBoolean(R.styleable.RoundButton_roundEnabled, true);
        int corners = typedArray.getDimensionPixelSize(R.styleable.RoundButton_corners, 10);
        boolean circular = typedArray.getBoolean(R.styleable.RoundButton_circular, false);
        borderWidth = typedArray.getDimensionPixelSize(R.styleable.RoundButton_border_Width, 0);
        borderColor = typedArray.getColor(R.styleable.RoundButton_border_Color, Color.parseColor("#555555"));
        borderColorPress = typedArray.getColor(R.styleable.RoundButton_border_ColorPress, Color.parseColor("#888888"));
        bgColor = typedArray.getColor(R.styleable.RoundButton_bgColor, Color.parseColor("#dddddd"));
        bgColorPress = typedArray.getColor(R.styleable.RoundButton_bgColorPress, Color.parseColor("#eeeeee"));

        typedArray.recycle();
        setGravity(Gravity.CENTER);
        roundHelper = new RoundHelper(corners, circular);
        setClickable(true);
        setFocusable(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        roundHelper.onSizeChanged(getWidth(), getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (roundEnable) {
            roundHelper.clipRound(canvas);
            if (pressed) {
                roundHelper.drawBgColor(canvas, bgColorPress);
            } else {
                roundHelper.drawBgColor(canvas, bgColor);
            }
            if(pressed){
                roundHelper.drawBorder(canvas,borderWidth,borderColorPress);
            }else{
                roundHelper.drawBorder(canvas,borderWidth,borderColor);
            }
        }
        super.onDraw(canvas);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                pressed = true;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                pressed = false;
                invalidate();
                break;
        }

        return super.dispatchTouchEvent(event);
    }
}
